package com.yanfan.zutai.controller;


import com.yanfan.common.annotation.Anonymous;
import com.yanfan.zutai.domain.BWeiXinUser;
import com.yanfan.zutai.domain.vo.BWeXinGet;
import com.yanfan.zutai.domain.vo.BWeXinMsg;
import com.yanfan.zutai.service.IBWeiXinUserService;
import com.yanfan.zutai.util.Detect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * @author cl
 */
@RestController
@RequestMapping(value = "/noTokenApi")
@Anonymous
public class BDeviceApiController {
    @Autowired
    private IBWeiXinUserService bWeiXinUserService;

    /**
     * 公众号推送消息
     */
    @PostMapping(value = "/weChatMsg", consumes = "text/xml", produces = "text/xml", headers = "content-type=multipart/form-data")
    public void weChatMsg(@RequestBody BWeXinMsg bWeXinMsg) throws Exception {
        if ("SCAN".equals(bWeXinMsg.getEvent())) {// 扫码关注
            if (Detect.notEmpty(bWeXinMsg.getEventKey())) {
                BWeiXinUser bWeiXinUser = new BWeiXinUser();
                String userName = bWeXinMsg.getEventKey().replace("qrscene_", "");
                bWeiXinUser.setUserName(userName);
                bWeiXinUser.setOpenId(bWeXinMsg.getFromUserName());
                List<BWeiXinUser> bWeiXinUsers = bWeiXinUserService.selectBWeiXinUserList(bWeiXinUser);
                if (bWeiXinUsers.size() == 0) {
                    bWeiXinUser.setSubscribe(1);
                    bWeiXinUser.setUpdateTime(new Date());
                    bWeiXinUserService.insertBWeiXinUser(bWeiXinUser);
                } else {
                    bWeiXinUser.setId(bWeiXinUsers.get(0).getId());
                    bWeiXinUser.setSubscribe(1);
                    bWeiXinUser.setUpdateTime(new Date());
                    bWeiXinUserService.updateBWeiXinUser(bWeiXinUser);
                }
            }
        } else if ("unsubscribe".equals(bWeXinMsg.getEvent())) {
            BWeiXinUser bWeiXinUser = new BWeiXinUser();
            bWeiXinUser.setOpenId(bWeXinMsg.getFromUserName());
            List<BWeiXinUser> bWeiXinUsers = bWeiXinUserService.selectBWeiXinUserList(bWeiXinUser);
            for (int i = 0; i < bWeiXinUsers.size(); i++) {
                BWeiXinUser xinUser = bWeiXinUsers.get(i);
                xinUser.setSubscribe(0);
                bWeiXinUser.setUpdateTime(new Date());
                bWeiXinUserService.updateBWeiXinUser(xinUser);
            }
        } else if ("subscribe".equals(bWeXinMsg.getEvent())) {
            BWeiXinUser bWeiXinUser = new BWeiXinUser();
            bWeiXinUser.setOpenId(bWeXinMsg.getFromUserName());
            String userName = bWeXinMsg.getEventKey().replace("qrscene_", "");
            bWeiXinUser.setUserName(userName);
            List<BWeiXinUser> bWeiXinUsers = bWeiXinUserService.selectBWeiXinUserList(bWeiXinUser);
            if (bWeiXinUsers.size() == 0) {
                bWeiXinUser.setSubscribe(1);
                bWeiXinUser.setUpdateTime(new Date());
                bWeiXinUserService.insertBWeiXinUser(bWeiXinUser);
            } else {
                BWeiXinUser weXinUser = bWeiXinUsers.get(0);
                bWeiXinUser.setSubscribe(1);
                bWeiXinUser.setId(weXinUser.getId());
                bWeiXinUser.setUpdateTime(new Date());
                bWeiXinUserService.updateBWeiXinUser(bWeiXinUser);
            }
        }
    }

    /**
     * 公众号推送消息
     */
    @GetMapping(value = "/weChatMsg")
    public String weChatMsg(BWeXinGet bWeXinGet) throws Exception {
        return bWeXinGet.getEchostr();
    }
}
